//
//  WBMainTabBarController.swift
//  盈微博
//
//  Created by hoterzhang on 2018/1/20.
//  Copyright © 2018年 hoterzhang. All rights reserved.
//

import UIKit

class WBMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
        setupComposeButton()
        // Do any additional setup after loading the view.
    }
    /**
      通过代码控制横竖屏
      landscape 横
      portrait 竖
    */
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
    // MARK: - 监听
    // FIXME: 没有实现
    @objc private func composeStatus() {
        print("点击按钮")
        //测试方向
        let vc = UIViewController()
        
        vc.view.backgroundColor = UIColor.cz_random()
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
    }
    
    // MARK: - 私有控件
    private lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add_highlighted", backgroundImageName: "tabbar_compose_button")
}

extension WBMainTabBarController {
    
    // 设置添加按钮
    private func setupComposeButton(){
        tabBar.addSubview(composeButton)
        let count:CGFloat = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width / count - 1 //降低容错点
        composeButton.frame = tabBar.bounds.insetBy(dx: 2*w, dy: 0) //调整dy 可以设置按钮的高度
        //监听按钮
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    private func setupChildControllers() {
        let array = [
            ["clsName": "WBHomeViewController", "title": "首页", "imageName": "home"],
            ["clsName": "WBMessageViewController", "title": "消息", "imageName": "message_center"],
            ["clsName": "UIViewController"],
            ["clsName": "WBDiscoverViewController", "title": "发现", "imageName": "discover"],
            ["clsName": "WBProfileViewController", "title": "我", "imageName": "profile"]
        ]
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    
    //
    private func controller(dict: [String: String]) -> UIViewController {
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else {
                return UIViewController()
        }
        // 创建视图控制器
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置字体
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.orange], for: .selected)
        
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)], for: .normal)
        let nav = WBMainNavigationController(rootViewController: vc)
        return nav
    }
}
